<template>
  <div class="strategy-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>策略管理</span>
          <el-button type="primary" size="small" @click="createStrategy">新建策略</el-button>
        </div>
      </template>
      <el-table :data="strategies" style="width: 100%">
        <el-table-column prop="id" label="策略ID" width="80" />
        <el-table-column prop="name" label="策略名称" width="180" />
        <el-table-column prop="description" label="策略描述" />
        <el-table-column prop="symbol" label="交易对" width="100" />
        <el-table-column prop="interval" label="周期" width="100" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="editStrategy(row.id)">编辑</el-button>
            <el-button type="success" size="small" @click="runBacktest(row.id)">回测</el-button>
            <el-button type="danger" size="small" @click="deleteStrategy(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';

const router = useRouter();
const strategies = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 模拟数据
const mockStrategies = [
  { id: 1, name: 'MACD交叉策略', description: '基于MACD指标的金叉死叉策略', symbol: 'BTCUSDT', interval: '1h' },
  { id: 2, name: 'RSI超买超卖策略', description: '基于RSI指标的超买超卖策略', symbol: 'ETHUSDT', interval: '4h' },
  { id: 3, name: '布林带策略', description: '基于布林带的突破策略', symbol: 'SOLUSDT', interval: '15m' }
];

onMounted(() => {
  fetchStrategies();
});

function fetchStrategies() {
  // 模拟API请求
  setTimeout(() => {
    strategies.value = mockStrategies;
    total.value = mockStrategies.length;
  }, 500);
}

function createStrategy() {
  router.push('/strategy/edit');
}

function editStrategy(id) {
  router.push(`/strategy/edit?id=${id}`);
}

function runBacktest(id) {
  router.push(`/backtest?strategyId=${id}`);
}

function deleteStrategy(id) {
  ElMessageBox.confirm(
    '确定要删除此策略吗？',
    '确认删除',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // 模拟删除操作
    setTimeout(() => {
      strategies.value = strategies.value.filter(item => item.id !== id);
      total.value = strategies.value.length;
      ElMessage({
        type: 'success',
        message: '策略删除成功'
      });
    }, 300);
  }).catch(() => {
    // 取消删除
  });
}

function handleSizeChange(val) {
  pageSize.value = val;
  fetchStrategies();
}

function handleCurrentChange(val) {
  currentPage.value = val;
  fetchStrategies();
}
</script>

<style scoped>
.strategy-list-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>